package com.roger.ats.init.cases;

import android.support.test.uiautomator.UiCollection;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiScrollable;
import android.support.test.uiautomator.UiSelector;
import com.roger.ats.init.util.LogUtils;
import com.roger.uia.helper.UiBase;
import com.roger.uia.helper.manager.Telephony;
import com.roger.uia.helper.util.StrUtils;
import static com.roger.ats.init.cases.SettingsInit.hot_seat;
import static com.roger.uia.helper.UiBase.checkPoint;
import static com.roger.uia.helper.UiBase.existObj;
import static com.roger.uia.helper.UiBase.mDevice;
import static com.roger.uia.helper.UiBase.objSelect;
import static com.roger.uia.helper.UiBase.waitClickObj;
import static com.roger.uia.helper.UiBase.waitObj;

public class HomeScreenInit {
    private static final String TAG = "Test" + HomeScreenInit.class.getSimpleName();
    private static String clock = "com.android.deskclock:id/clock";
    private static String searchBox="com.google.android.googlequicksearchbox:id/search_edit_frame";
    private static LogUtils logUtils = new LogUtils();

    public static void removeWidget(){
        logUtils.printLog("==start remove widget==");
        int y = 50;
        for(int i=0;i<3;i++){
            mDevice.pressHome();
            if(UiBase.existObj(UiBase.objSelect[3],clock)) {
                try {
                    UiBase.dragObj(UiBase.objSelect[3],clock,StrUtils.SCREEN_W/2,y,50);
                } catch (Throwable e) {
                    e.printStackTrace();
                }
                y += 10;
                if(i==2) logUtils.printLog("remove widget clock fail");
            }else {
                logUtils.printLog("remove widget clock success");
                break;
            }
        }

        y = 50;
        for(int i=0;i<3;i++){
            mDevice.pressHome();
            if(UiBase.existObj(UiBase.objSelect[4],"联想天气")) {
                try {
                    UiBase.dragObj(UiBase.objSelect[4],"联想天气",StrUtils.SCREEN_W/2,y,50);
                } catch (Throwable e) {
                    e.printStackTrace();
                }
                y += 10;
                if(i==2) logUtils.printLog("remove widget 联想天气 fail");
            }else {
                logUtils.printLog("remove widget 联想天气 success");
                break;
            }
        }

        y = 50;
        for(int i=0;i<3;i++){
            mDevice.pressHome();
            if(UiBase.existObj(UiBase.objSelect[3],searchBox)) {
                try {
                    UiBase.dragObj(UiBase.objSelect[3],searchBox,StrUtils.SCREEN_W/2,y,50);
                } catch (Throwable e) {
                    e.printStackTrace();
                }
                y += 10;
                if(i==2) logUtils.printLog("remove widget search box fail");
            }else {
                logUtils.printLog("remove widget search box success");
                break;
            }
        }
    }

    public static void openLenovoEntertainment(){
        mDevice.pressHome();
        logUtils.printLog("==start open Lenovo Entertainment==");
        UiCollection uic = new UiCollection(new UiSelector().resourceId("android:id/list"));
        for(int i=0;i<3;i++){
            try{
                mDevice.swipe(StrUtils.SCREEN_W/2,150,StrUtils.SCREEN_W/2,150,200);
                UiBase.waitClickObj(UiBase.objSelect[0],"主屏幕设置",3);
                UiBase.waitObjCheck(UiBase.objSelect[0],"智慧联想",3);
                UiObject parent = UiBase.getObj(UiBase.objSelect[0],"智慧联想").getFromParent(new UiSelector());
                UiObject btn = UiBase.getObjSameLine(uic,parent,UiBase.getObj(UiBase.objSelect[3],"android:id/switch_widget"));
                if(btn.getText().equals("关闭")) btn.clickAndWaitForNewWindow();
                UiBase.checkPoint("check status",btn.getText().equals("开启"));
                logUtils.printLog("open Lenovo Entertainment success");
                break;
            }catch (Throwable e){
                if(UiBase.existObj(UiBase.objSelect[3],"android:id/list")){
                    logUtils.printLog("not find Lenovo Entertainment");
                    break;
                }else {
                    logUtils.printLog("open Lenovo Entertainment fail");
                }
            }finally {
                mDevice.pressHome();
            }
        }
    }

    public static void checkLeftScreen(){
        mDevice.pressHome();
        logUtils.printLog("==start check left screen==");
        try {
            UiBase.waitObj(UiBase.objSelect[3],hot_seat,3);
            mDevice.swipe(0,StrUtils.SCREEN_H/2,StrUtils.SCREEN_W,StrUtils.SCREEN_H/2,50);
            UiBase.waitClickIfObjExist(UiBase.objSelect[2],StrUtils.allow,10);
        } catch (Throwable e) {
            e.printStackTrace();
        }finally {
            logUtils.printLog("check left screen finished");
            mDevice.pressHome();
        }
    }

    public static void setShortCut(){
        UiScrollable list = new UiScrollable(new UiSelector().resourceId("android:id/list"));
        String[] icons = {"录屏","勿扰","飞行模式"};
        int x,y;
        mDevice.pressHome();
        logUtils.printLog("==start set shortcut==");
        for (int i=0;i<3;i++){
            try{
                mDevice.openQuickSettings();
                UiBase.waitClickObj(UiBase.objSelect[4],"修改设置顺序。",3);
                UiBase.waitObj(UiBase.objSelect[0],"按住并拖动即可添加图块",3);
                int hash = UiBase.getObj(UiBase.objSelect[0],"录屏").getBounds().bottom -
                        UiBase.getObj(UiBase.objSelect[0],"录屏").getBounds().top;
                if(!UiBase.existObj(UiBase.objSelect[5],"位置 6，Dolby")){
                    list.scrollToEnd(2000);
                    UiBase.getObj(UiBase.objSelect[0],"Dolby").dragTo(UiBase.getObj(UiBase.objSelect[3],"android:id/list").getBounds().right,UiBase.getObj(UiBase.objSelect[3],"android:id/list").getBounds().top,500);
                }
                for(String icon : icons){
                    list.scrollToEnd(2000);
                    x = UiBase.getObj(UiBase.objSelect[0],icon).getBounds().centerX();
                    int icon_y = UiBase.getObj(UiBase.objSelect[0],icon).getBounds().bottom;
                    y = UiBase.getObj(UiBase.objSelect[0],"按住并拖动即可添加图块").getBounds().bottom + hash - 5 - 2 * i;
                    if(icon_y > y ) UiBase.getObj(UiBase.objSelect[0],icon).dragTo(x,y,150);
                }
                y = UiBase.getObj(UiBase.objSelect[0],"按住并拖动即可添加图块").getBounds().bottom;
                for(String icon : icons) {
                    UiBase.checkPoint("check status", UiBase.getObj(UiBase.objSelect[0], icon).getBounds().bottom < y);
                }
                UiBase.clickObj(UiBase.objSelect[0],"完成");
                mDevice.openQuickSettings();
                UiBase.waitClickObj(UiBase.objSelect[4],"修改设置顺序。",3);
                UiBase.waitObj(UiBase.objSelect[0],"按住并拖动即可添加图块",3);
                UiBase.checkPoint("check status",UiBase.existObj(UiBase.objSelect[5],"位置 6，Dolby"));
                logUtils.printLog("set shortcut success");
                break;
            }catch (Throwable e){
                logUtils.printLog("set shortcut fail");
            }finally {
                mDevice.pressHome();
            }
        }
    }

    public static void setRecordVideo(){
        mDevice.pressHome();
        logUtils.printLog("==start set record video==");
        for (int i=0;i<3;i++){
            try{
                mDevice.openQuickSettings();
                int y = UiBase.getObjY(UiBase.objSelect[3],"com.android.systemui:id/tile_page");
                if(!UiBase.existObj(UiBase.objSelect[4],"录屏")){
                    mDevice.swipe(StrUtils.SCREEN_W -20 ,y,20,y,50);
                }
                UiBase.waitClickObj(UiBase.objSelect[4],"录屏",5);
                UiBase.waitClickIfObjExist(UiBase.objSelect[0],"允许",5);
                //UiBase.waitClickIfObjExist(UiBase.objSelect[0],"不再显示",5);
                UiBase.waitClickIfObjExist(UiBase.objSelect[0],"立即开始",5);
                mDevice.openQuickSettings();
                if(!UiBase.existObj(UiBase.objSelect[4],"录屏")){
                    mDevice.swipe(StrUtils.SCREEN_W -20 ,y,20,y,50);
                }
                UiBase.checkPoint("check status",UiBase.getTxtFromObj(UiBase.objSelect[4],"录屏").equals("开启"));
                UiBase.waitClickObj(UiBase.objSelect[4],"录屏",5);
                logUtils.printLog("set record video success");
                break;
            }catch (Throwable e){
                logUtils.printLog("set record video fail");
            }finally {
                mDevice.pressHome();
            }
        }
    }

    public static void setAmarShortCut(){
        UiScrollable list = new UiScrollable(new UiSelector().resourceId("android:id/list"));
        String[] icons = {"录屏","勿扰","飞行模式"};
        int x,y;
        mDevice.pressHome();
        logUtils.printLog("==start set shortcut==");
        for (int i=0;i<3;i++){
            try{
                mDevice.openQuickSettings();
                UiBase.waitClickObj(UiBase.objSelect[4],"修改设置顺序。",3);
                UiBase.waitObj(UiBase.objSelect[0],"按住并拖动即可添加图块",3);
                int hash = UiBase.getObj(UiBase.objSelect[0],"录屏").getBounds().bottom -
                        UiBase.getObj(UiBase.objSelect[0],"录屏").getBounds().top;
//               判断第三行的高度
                UiObject thirteenth = UiBase.getObj(UiBase.objSelect[5], "13");
                int center_y = thirteenth.getBounds().centerY();
                int bottom_y = thirteenth.getBounds().bottom;
                int right_x = thirteenth.getBounds().right;

                if(!UiBase.existObj(UiBase.objSelect[5],"位置 6，Dolby")){
                    list.scrollToEnd(2000);
                    UiBase.getObj(UiBase.objSelect[0],"Dolby").dragTo(UiBase.getObj(UiBase.objSelect[3],"android:id/list").getBounds().right,UiBase.getObj(UiBase.objSelect[3],"android:id/list").getBounds().top,500);
                }
                for(String icon : icons){
                    list.scrollToEnd(2000);
//                    x = UiBase.getObj(UiBase.objSelect[0],icon).getBounds().centerX();
                    int icon_y = UiBase.getObj(UiBase.objSelect[0],icon).getBounds().bottom;
//                    y = UiBase.getObj(UiBase.objSelect[0],"按住并拖动即可添加图块").getBounds().bottom + hash - 5 - 2 * i;
                    if (icon_y < center_y | icon_y > bottom_y)
                        UiBase.getObj(UiBase.objSelect[0], icon).dragTo(right_x + hash, center_y + hash, 150);
//                    if(icon_y > y ) UiBase.getObj(UiBase.objSelect[0],icon).dragTo(x,y,150);
                }
//                y = UiBase.getObj(UiBase.objSelect[0],"按住并拖动即可添加图块").getBounds().bottom;
                for(String icon : icons) {
                    UiBase.checkPoint("check status", UiBase.getObj(UiBase.objSelect[0], icon).getBounds().centerY() < bottom_y);
                }
                UiBase.clickObj(UiBase.objSelect[0],"完成");
                mDevice.openQuickSettings();
                UiBase.waitClickObj(UiBase.objSelect[4],"修改设置顺序。",3);
                UiBase.waitObj(UiBase.objSelect[0],"按住并拖动即可添加图块",3);
                UiBase.checkPoint("check status",UiBase.existObj(UiBase.objSelect[5],"位置 6，Dolby"));
                //改变Dolby 状态为动态
                mDevice.pressHome();
                mDevice.openQuickSettings();
                if (!UiBase.existObj(objSelect[0], "动态")) {
                    waitClickObj(objSelect[0], "自定义", 3);
                    waitObj(objSelect[3], "android:id/list", 3);
                    waitClickObj(objSelect[0], "动态", 3);
                    waitClickObj(objSelect[0], "完成", 3);
                }
                checkPoint("check status",existObj(objSelect[0], "动态"));
                logUtils.printLog("set shortcut success");
                break;
            }catch (Throwable e){
                logUtils.printLog("set shortcut fail");
            }finally {
                mDevice.pressHome();
            }
        }
    }

    public static void setAmarRecordVideo(){
        mDevice.pressHome();
        logUtils.printLog("==start set record video==");
        for (int i=0;i<3;i++){
            try{
                mDevice.openQuickSettings();
//                int y = UiBase.getObjY(UiBase.objSelect[3],"com.android.systemui:id/tile_page");
                UiObject quickSetting = UiBase.getObj(UiBase.objSelect[3], "com.android.systemui:id/quick_settings_panel");
                int quick_right = quickSetting.getBounds().right;
                int quick_center_y = quickSetting.getBounds().centerY();
                if(!UiBase.existObj(UiBase.objSelect[4],"录屏")){
                    mDevice.swipe(quick_right/4 * 3 ,quick_center_y,20,quick_center_y,50);
                }
                UiBase.waitClickObj(UiBase.objSelect[4],"录屏",5);
                UiBase.waitClickIfObjExist(UiBase.objSelect[0],"允许",5);
                //UiBase.waitClickIfObjExist(UiBase.objSelect[0],"不再显示",5);
                UiBase.waitClickIfObjExist(UiBase.objSelect[0],"立即开始",5);
                mDevice.openQuickSettings();
                if(!UiBase.existObj(UiBase.objSelect[4],"录屏")){
                    mDevice.swipe(quick_right/4 * 3 ,quick_center_y,20,quick_center_y,50);
                }
                UiBase.checkPoint("check status",UiBase.getTxtFromObj(UiBase.objSelect[4],"录屏").equals("开启"));
                UiBase.waitClickObj(UiBase.objSelect[4],"录屏",5);
                logUtils.printLog("set record video success");
                break;
            }catch (Throwable e){
                logUtils.printLog("set record video fail");
            }finally {
                mDevice.pressHome();
            }
        }
    }

    public static void setLTE_network() {
        mDevice.pressHome();
        logUtils.printLog("==start set LTE network==");
        try {
            if (Telephony.tel().hasSIM()) {
                mDevice.openQuickSettings();
                if (UiBase.getTxtFromObj(objSelect[5], "移动数据").equals("关闭")) {
                    waitClickObj(objSelect[5], "移动数据", 3);
                } else {
                    logUtils.printLog("Mobile data already open");
                }
                checkPoint("check status", UiBase.getTxtFromObj(objSelect[5], "移动数据").equals("开启"));
                logUtils.printLog("set mobile data Success");
            } else {
                logUtils.printLog("no SIM");
            }

        } catch (Throwable e) {
            logUtils.printLog("set LTE network fail");
        }finally {
            mDevice.pressHome();
        }
    }

}